home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / g_quake / shockab.zip / AI.QC next >
Text File  |  1996-10-07  |  16KB  |  753 lines

  1. void() movetarget_f;
  2. void() t_movetarget;
  3. void() knight_walk1;
  4. void() knight_bow6;
  5. void() knight_bow1;
  6. void(entity etemp, entity stemp, entity stemp, float dmg) T_Damage;
  7. /*
  8.  
  9. .enemy
  10. Will be world if not currently angry at anyone.
  11.  
  12. .movetarget
  13. The next path spot to walk toward.  If .enemy, ignore .movetarget.
  14. When an enemy is killed, the monster will try to return to it's path.
  15.  
  16. .huntt_ime
  17. Set to time + something when the player is in sight, but movement straight for
  18. him is blocked.  This causes the monster to use wall following code for
  19. movement direction instead of sighting on the player.
  20.  
  21. .ideal_yaw
  22. A yaw angle of the intended direction, which will be turned towards at up
  23. to 45 deg / state.  If the enemy is in view and hunt_time is not active,
  24. this will be the exact line towards the enemy.
  25.  
  26. .pausetime
  27. A monster will leave it's stand state and head towards it's .movetarget when
  28. time > .pausetime.
  29.  
  30. walkmove(angle, speed) primitive is all or nothing
  31. */
  32.  
  33.  
  34. //
  35. // globals
  36. //
  37. float current_yaw;
  38.  
  39. //
  40. // when a monster becomes angry at a player, that monster will be used
  41. // as the sight target the next frame so that monsters near that one
  42. // will wake up even if they wouldn't have noticed the player
  43. //
  44. entity      sight_entity;
  45. float sight_entity_time;
  46.  
  47. float(float v) anglemod =
  48. {
  49.     while (v >= 360)
  50.         v = v - 360;
  51.     while (v < 0)
  52.         v = v + 360;
  53.     return v;
  54. };
  55.  
  56. /*
  57. ==============================================================================
  58.  
  59. MOVETARGET CODE
  60.  
  61. The angle of the movetarget effects standing and bowing direction, but has no effect on movement, which allways heads to the next target.
  62.  
  63. targetname
  64. must be present.  The name of this movetarget.
  65.  
  66. target
  67. the next spot to move to.  If not present, stop here for good.
  68.  
  69. pausetime
  70. The number of seconds to spend standing or bowing for path_stand or path_bow
  71.  
  72. ==============================================================================
  73. */
  74.  
  75.  
  76. void() movetarget_f =
  77. {
  78.     if (!self.targetname)
  79.         objerror ("monster_movetarget: no targetname");
  80.         
  81.     self.solid = SOLID_TRIGGER;
  82.     self.touch = t_movetarget;
  83.     setsize (self, '-8 -8 -8', '8 8 8');
  84.     
  85. };
  86.  
  87. /*QUAKED path_corner (0.5 0.3 0) (-8 -8 -8) (8 8 8)
  88. Monsters will continue walking towards the next target corner.
  89. */
  90. void() path_corner =
  91. {
  92.     movetarget_f ();
  93. };
  94.  
  95.  
  96. /*
  97. =============
  98. t_movetarget
  99.  
  100. Something has bumped into a movetarget.  If it is a monster
  101. moving towards it, change the next destination and continue.
  102. ==============
  103. */
  104. void() t_movetarget =
  105. {
  106. local entity      temp;
  107.  
  108.     if (other.movetarget != self)
  109.         return;
  110.     
  111.     if (other.enemy)
  112.         return;           // fighting, not following a path
  113.  
  114.     temp = self;
  115.     self = other;
  116.     other = temp;
  117.  
  118.     if (self.classname == "monster_ogre")
  119.         sound (self, CHAN_VOICE, "ogre/ogdrag.wav", 1, ATTN_IDLE);// play chainsaw drag sound
  120.  
  121. //dprint ("t_movetarget\n");
  122.     self.goalentity = self.movetarget = find (world, targetname, other.target);
  123.     self.ideal_yaw = vectoyaw(self.goalentity.origin - self.origin);
  124.     if (!self.movetarget)
  125.     {
  126.         self.pausetime = time + 999999;
  127.         self.th_stand ();
  128.         return;
  129.     }
  130. };
  131.  
  132.  
  133.  
  134. //============================================================================
  135.  
  136. /*
  137. =============
  138. range
  139.  
  140. returns the range catagorization of an entity reletive to self
  141. 0     melee range, will become hostile even if back is turned
  142. 1     visibility and infront, or visibility and show hostile
  143. 2     infront and show hostile
  144. 3     only triggered by damage
  145. =============
  146. */
  147. float(entity targ) range =
  148. {
  149. local vector      spot1, spot2;
  150. local float       r;    
  151.         
  152.     spot1 = self.origin + self.view_ofs;
  153.     spot2 = targ.origin + targ.view_ofs;
  154.     
  155.     r = vlen (spot1 - spot2);
  156.     if (r < 120)
  157.         return RANGE_MELEE;
  158.     if (r < 500)
  159.         return RANGE_NEAR;
  160.     if (r < 1000)
  161.         return RANGE_MID;
  162.     return RANGE_FAR;
  163. };
  164.  
  165. /*
  166. =============
  167. visible
  168.  
  169. returns 1 if the entity is visible to self, even if not infront ()
  170. =============
  171. */
  172. float (entity targ) visible =
  173. {
  174.     local vector      spot1, spot2;
  175.     
  176.     spot1 = self.origin + self.view_ofs;
  177.     spot2 = targ.origin + targ.view_ofs;
  178.     traceline (spot1, spot2, TRUE, self);     // see through other monsters
  179.     
  180.     if (trace_inopen && trace_inwater)
  181.         return FALSE;                 // sight line crossed contents
  182.  
  183.     if (trace_fraction == 1)
  184.         return TRUE;
  185.     return FALSE;
  186. };
  187.  
  188.  
  189. /*
  190. =============
  191. infront
  192.  
  193. returns 1 if the entity is in front (in sight) of self
  194. =============
  195. */
  196. float(entity targ) infront =
  197. {
  198.     local vector      vec;
  199.     local float       dot;
  200.     
  201.     makevectors (self.angles);
  202.     vec = normalize (targ.origin - self.origin);
  203.     dot = vec * v_forward;
  204.     
  205.     if ( dot > 0.3)
  206.     {
  207.         return TRUE;
  208.     }
  209.     return FALSE;
  210. };
  211.  
  212.  
  213. //============================================================================
  214.  
  215. /*
  216. ===========
  217. ChangeYaw
  218.  
  219. Turns towards self.ideal_yaw at self.yaw_speed
  220. Sets the global variable current_yaw
  221. Called every 0.1 sec by monsters
  222. ============
  223. */
  224. /*
  225.  
  226. void() ChangeYaw =
  227. {
  228.     local float       ideal, move;
  229.  
  230. //current_yaw = self.ideal_yaw;
  231. // mod down the current angle
  232.     current_yaw = anglemod( self.angles_y );
  233.     ideal = self.ideal_yaw;
  234.     
  235.     if (current_yaw == ideal)
  236.         return;
  237.     
  238.     move = ideal - current_yaw;
  239.     if (ideal > current_yaw)
  240.     {
  241.         if (move > 180)
  242.             move = move - 360;
  243.     }
  244.     else
  245.     {
  246.         if (move < -180)
  247.             move = move + 360;
  248.     }
  249.         
  250.     if (move > 0)
  251.     {
  252.         if (move > self.yaw_speed)
  253.             move = self.yaw_speed;
  254.     }
  255.     else
  256.     {
  257.         if (move < 0-self.yaw_speed )
  258.             move = 0-self.yaw_speed;
  259.     }
  260.  
  261.     current_yaw = anglemod (current_yaw + move);
  262.  
  263.     self.angles_y = current_yaw;
  264. };
  265.  
  266. */
  267.  
  268.  
  269. //============================================================================
  270.  
  271. void() HuntTarget =
  272. {
  273.     self.goalentity = self.enemy;
  274.     self.think = self.th_run;
  275.     self.ideal_yaw = vectoyaw(self.enemy.origin - self.origin);
  276.     self.nextthink = time + 0.1;
  277.     SUB_AttackFinished (1); // wait a while before first attack
  278. };
  279.  
  280. void() SightSound =
  281. {
  282. local float rsnd;
  283.  
  284.     if (self.classname == "monster_ogre")     
  285.         sound (self, CHAN_VOICE, "ogre/ogwake.wav", 1, ATTN_NORM);
  286.     else if (self.classname == "monster_knight")
  287.         sound (self, CHAN_VOICE, "knight/ksight.wav", 1, ATTN_NORM);
  288.     else if (self.classname == "monster_shambler")
  289.         sound (self, CHAN_VOICE, "shambler/ssight.wav", 1, ATTN_NORM);
  290.     else if (self.classname == "monster_demon1")
  291.         sound (self, CHAN_VOICE, "demon/sight2.wav", 1, ATTN_NORM);
  292.     else if (self.classname == "monster_wizard")
  293.         sound (self, CHAN_VOICE, "wizard/wsight.wav", 1, ATTN_NORM);
  294.     else if (self.classname == "monster_zombie")
  295.         sound (self, CHAN_VOICE, "zombie/z_idle.wav", 1, ATTN_NORM);
  296.     else if (self.classname == "monster_dog")
  297.         sound (self, CHAN_VOICE, "dog/dsight.wav", 1, ATTN_NORM);
  298.     else if (self.classname == "monster_hell_knight")
  299.         sound (self, CHAN_VOICE, "hknight/sight1.wav", 1, ATTN_NORM);
  300.     else if (self.classname == "monster_tarbaby")
  301.         sound (self, CHAN_VOICE, "blob/sight1.wav", 1, ATTN_NORM);
  302.     else if (self.classname == "monster_vomit")
  303.         sound (self, CHAN_VOICE, "vomitus/v_sight1.wav", 1, ATTN_NORM);
  304.     else if (self.classname == "monster_enforcer")
  305.     {
  306.         rsnd = rint(random() * 3);                
  307.         if (rsnd == 1)
  308.             sound (self, CHAN_VOICE, "enforcer/sight1.wav", 1, ATTN_NORM);
  309.         else if (rsnd == 2)
  310.             sound (self, CHAN_VOICE, "enforcer/sight2.wav", 1, ATTN_NORM);
  311.         else if (rsnd == 0)
  312.             sound (self, CHAN_VOICE, "enforcer/sight3.wav", 1, ATTN_NORM);
  313.         else
  314.             sound (self, CHAN_VOICE, "enforcer/sight4.wav", 1, ATTN_NORM);
  315.     }
  316.     else if (self.classname == "monster_army")
  317.         sound (self, CHAN_VOICE, "soldier/sight1.wav", 1, ATTN_NORM);
  318.     else if (self.classname == "monster_shalrath")
  319.         sound (self, CHAN_VOICE, "shalrath/sight.wav", 1, ATTN_NORM);
  320. };
  321.  
  322. void() FoundTarget =
  323. {
  324.     if (self.enemy.classname == "player")
  325.     {     // let other monsters see this monster for a while
  326.         sight_entity = self;
  327.         sight_entity_time = time;
  328.     }
  329.     
  330.     self.show_hostile = time + 1;       // wake up other monsters
  331.  
  332.     SightSound ();
  333.     HuntTarget ();
  334. };
  335.  
  336. /*
  337. ===========
  338. FindTarget
  339.  
  340. Self is currently not attacking anything, so try to find a target
  341.  
  342. Returns TRUE if an enemy was sighted
  343.  
  344. When a player fires a missile, the point of impact becomes a fakeplayer so
  345. that monsters that see the impact will respond as if they had seen the
  346. player.
  347.  
  348. To avoid spending too much time, only a single client (or fakeclient) is
  349. checked each frame.  This means multi player games will have slightly
  350. slower noticing monsters.
  351. ============
  352. */
  353. float() FindTarget =
  354. {
  355.     local entity      client;
  356.     local float       r;
  357.  
  358. // if the first spawnflag bit is set, the monster will only wake up on
  359. // really seeing the player, not another monster getting angry
  360.  
  361. // spawnflags & 3 is a big hack, because zombie crucified used the first
  362. // spawn flag prior to the ambush flag, and I forgot about it, so the second
  363. // spawn flag works as well
  364.     if (sight_entity_time >= time - 0.1 && !(self.spawnflags & 3) )
  365.     {
  366.         client = sight_entity;
  367.         if (client.enemy == self.enemy)
  368.             return;
  369.     }
  370.     else
  371.     {
  372.         client = checkclient ();
  373.         if (!client)
  374.             return FALSE;     // current check entity isn't in PVS
  375.     }
  376.  
  377.     if (client == self.enemy)
  378.         return FALSE;
  379.  
  380.     if (client.flags & FL_NOTARGET)
  381.         return FALSE;
  382.     if (client.items & IT_INVISIBILITY)
  383.         return FALSE;
  384.  
  385.     r = range (client);
  386.     if (r == RANGE_FAR)
  387.         return FALSE;
  388.         
  389.     if (!visible (client))
  390.         return FALSE;
  391.  
  392.     if (r == RANGE_NEAR)
  393.     {
  394.         if (client.show_hostile < time && !infront (client))
  395.             return FALSE;
  396.     }
  397.     else if (r == RANGE_MID)
  398.     {
  399.         if ( /* client.show_hostile < time || */ !infront (client))
  400.             return FALSE;
  401.     }
  402.     
  403. //
  404. // got one
  405. //
  406.     self.enemy = client;
  407.     if (self.enemy.classname != "player")
  408.     {
  409.         self.enemy = self.enemy.enemy;
  410.         if (self.enemy.classname != "player")
  411.         {
  412.             self.enemy = world;
  413.             return FALSE;
  414.         }
  415.     }
  416.     
  417.     FoundTarget ();
  418.  
  419.     return TRUE;
  420. };
  421.  
  422.  
  423. //=============================================================================
  424.  
  425. void(float dist) ai_forward =
  426. {
  427.     walkmove (self.angles_y, dist);
  428. };
  429.  
  430. void(float dist) ai_back =
  431. {
  432.     walkmove ( (self.angles_y+180), dist);
  433. };
  434.  
  435.  
  436. /*
  437. =============
  438. ai_pain
  439.  
  440. stagger back a bit
  441. =============
  442. */
  443. void(float dist) ai_pain =
  444. {
  445.     ai_back (dist);
  446. /*
  447.     local float away;
  448.     
  449.     away = anglemod (vectoyaw (self.origin - self.enemy.origin) 
  450.     + 180*(random()- 0.5) );
  451.     
  452.     walkmove (away, dist);
  453. */
  454. };
  455.  
  456. /*
  457. =============
  458. ai_painforward
  459.  
  460. stagger back a bit
  461. =============
  462. */
  463. void(float dist) ai_painforward =
  464. {
  465.     walkmove (self.ideal_yaw, dist);
  466. };
  467.  
  468. /*
  469. =============
  470. ai_walk
  471.  
  472. The monster is walking it's beat
  473. =============
  474. */
  475. void(float dist) ai_walk =
  476. {
  477.     local vector            mtemp;
  478.     
  479.     movedist = dist;
  480.     
  481.     if (self.classname == "monster_dragon")
  482.     {
  483.         movetogoal (dist);
  484.         return;
  485.     }
  486.     // check for noticing a player
  487.     if (FindTarget ())
  488.         return;
  489.  
  490.     movetogoal (dist);
  491. };
  492.  
  493.  
  494. /*
  495. =============
  496. ai_stand
  497.  
  498. The monster is staying in one place for a while, with slight angle turns
  499. =============
  500. */
  501. void() ai_stand =
  502. {
  503.     if (FindTarget ())
  504.         return;
  505.     
  506.     if (time > self.pausetime)
  507.     {
  508.         self.th_walk ();
  509.         return;
  510.     }
  511.     
  512. // change angle slightly
  513.  
  514. };
  515.  
  516. /*
  517. =============
  518. ai_turn
  519.  
  520. don't move, but turn towards ideal_yaw
  521. =============
  522. */
  523. void() ai_turn =
  524. {
  525.     if (FindTarget ())
  526.         return;
  527.     
  528.     ChangeYaw ();
  529. };
  530.  
  531. //=============================================================================
  532.  
  533. /*
  534. =============
  535. ChooseTurn
  536. =============
  537. */
  538. void(vector dest3) ChooseTurn =
  539. {
  540.     local vector      dir, newdir;
  541.     
  542.     dir = self.origin - dest3;
  543.  
  544.     newdir_x = trace_plane_normal_y;
  545.     newdir_y = 0 - trace_plane_normal_x;
  546.     newdir_z = 0;
  547.     
  548.     if (dir * newdir > 0)
  549.     {
  550.         dir_x = 0 - trace_plane_normal_y;
  551.         dir_y = trace_plane_normal_x;
  552.     }
  553.     else
  554.     {
  555.         dir_x = trace_plane_normal_y;
  556.         dir_y = 0 - trace_plane_normal_x;
  557.     }
  558.  
  559.     dir_z = 0;
  560.     self.ideal_yaw = vectoyaw(dir);     
  561. };
  562.  
  563. /*
  564. ============
  565. FacingIdeal
  566.  
  567. ============
  568. */
  569. float() FacingIdeal =
  570. {
  571.     local float delta;
  572.     
  573.     delta = anglemod(self.angles_y - self.ideal_yaw);
  574.     if (delta > 45 && delta < 315)
  575.         return FALSE;
  576.     return TRUE;
  577. };
  578.  
  579.  
  580. //=============================================================================
  581.  
  582. float()     WizardCheckAttack;
  583. float()     DogCheckAttack;
  584.  
  585. float() CheckAnyAttack =
  586. {
  587.     if (!enemy_vis)
  588.         return;
  589.     if (self.classname == "monster_army")
  590.         return SoldierCheckAttack ();
  591.     if (self.classname == "monster_ogre")
  592.         return OgreCheckAttack ();
  593.     if (self.classname == "monster_shambler")
  594.         return ShamCheckAttack ();
  595.     if (self.classname == "monster_demon1")
  596.         return DemonCheckAttack ();
  597.     if (self.classname == "monster_dog")
  598.         return DogCheckAttack ();
  599.     if (self.classname == "monster_wizard")
  600.         return WizardCheckAttack ();
  601.     return CheckAttack ();
  602. };
  603.  
  604.  
  605. /*
  606. =============
  607. ai_run_melee
  608.  
  609. Turn and close until within an angle to launch a melee attack
  610. =============
  611. */
  612. void() ai_run_melee =
  613. {
  614.     self.ideal_yaw = enemy_yaw;
  615.     ChangeYaw ();
  616.  
  617.     if (FacingIdeal())
  618.     {
  619.         self.th_melee ();
  620.         self.attack_state = AS_STRAIGHT;
  621.     }
  622. };
  623.  
  624.  
  625. /*
  626. =============
  627. ai_run_missile
  628.  
  629. Turn in place until within an angle to launch a missile attack
  630. =============
  631. */
  632. void() ai_run_missile =
  633. {
  634.     self.ideal_yaw = enemy_yaw;
  635.     ChangeYaw ();
  636.     if (FacingIdeal())
  637.     {
  638.         self.th_missile ();
  639.         self.attack_state = AS_STRAIGHT;
  640.     }
  641. };
  642.  
  643.  
  644. /*
  645. =============
  646. ai_run_slide
  647.  
  648. Strafe sideways, but stay at aproximately the same range
  649. =============
  650. */
  651. void() ai_run_slide =
  652. {
  653.     local float ofs;
  654.     
  655.     self.ideal_yaw = enemy_yaw;
  656.     ChangeYaw ();
  657.     if (self.lefty)
  658.         ofs = 90;
  659.     else
  660.         ofs = -90;
  661.     
  662.     if (walkmove (self.ideal_yaw + ofs, movedist))
  663.         return;
  664.         
  665.     self.lefty = 1 - self.lefty;
  666.     
  667.     walkmove (self.ideal_yaw - ofs, movedist);
  668. };
  669.  
  670.  
  671. /*
  672. =============
  673. ai_run
  674.  
  675. The monster has an enemy it is trying to kill
  676. =============
  677. */
  678. void(float dist) ai_run =
  679. {
  680.     local vector      delta;
  681.     local float axis;
  682.     local float direct, ang_rint, ang_floor, ang_ceil;
  683.     
  684.     CheckForDamage ();
  685.     if (self.health < 1)      
  686.         return;
  687.  
  688.     movedist = dist;
  689. // see if the enemy is dead
  690.     if (self.enemy.health <= 0)
  691.     {
  692.         self.enemy = world;
  693.     // FIXME: look all around for other targets
  694.         if (self.oldenemy.health > 0)
  695.         {
  696.             self.enemy = self.oldenemy;
  697.             HuntTarget ();
  698.         }
  699.         else
  700.         {
  701.             if (self.movetarget)
  702.                 self.th_walk ();
  703.             else
  704.                 self.th_stand ();
  705.             return;
  706.         }
  707.     }
  708.  
  709.     self.show_hostile = time + 1;       // wake up other monsters
  710.  
  711. // check knowledge of enemy
  712.     enemy_vis = visible(self.enemy);
  713.     if (enemy_vis)
  714.         self.search_time = time + 5;
  715.  
  716. // look for other coop players
  717.     if (coop && self.search_time < time)
  718.     {
  719.         if (FindTarget ())
  720.             return;
  721.     }
  722.  
  723.     enemy_infront = infront(self.enemy);
  724.     enemy_range = range(self.enemy);
  725.     enemy_yaw = vectoyaw(self.enemy.origin - self.origin);
  726.     
  727.     if (self.attack_state == AS_MISSILE)
  728.     {
  729. //dprint ("ai_run_missile\n");
  730.         ai_run_missile ();
  731.         return;
  732.     }
  733.     if (self.attack_state == AS_MELEE)
  734.     {
  735. //dprint ("ai_run_melee\n");
  736.         ai_run_melee ();
  737.         return;
  738.     }
  739.  
  740.     if (CheckAnyAttack ())
  741.         return;                             // beginning an attack
  742.         
  743.     if (self.attack_state == AS_SLIDING)
  744.     {
  745.         ai_run_slide ();
  746.         return;
  747.     }
  748.         
  749. // head straight in
  750.     movetogoal (dist);            // done in C code...
  751. };
  752.  
  753.